﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;

public partial class Admin_Views_module_category_list : System.Web.UI.UserControl
{
    CategoryBus cateBus = new CategoryBus();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.QueryString["success"]))
        {
            success.Visible = true;
            successText.Text = Request.QueryString["success"].ToString();
        }
        if (!string.IsNullOrEmpty(Request.QueryString["warning"]))
        {
            warning.Visible = true;
            warningText.Text = Request.QueryString["warning"].ToString();
        }
        if (!Page.IsPostBack)
            LoadCategory();
    }

    public void LoadCategory()
    {
        DataTable cateList = cateBus.GetAllCategory();

        DataTable dt = new DataTable();
        DataColumn dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "category_id";
        dt.Columns.Add(dc);

        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "category_name";
        dt.Columns.Add(dc);

        dc = new DataColumn();
        dc.DataType = Type.GetType("System.String");
        dc.ColumnName = "category_des";
        dt.Columns.Add(dc);

        for (int i = 0; i < cateList.Rows.Count; i++)
        {
            string name = "";
            if (Convert.ToInt32(cateList.Rows[i]["parent_id"].ToString()) > 0)
            {
                name += GetChildCategory(Convert.ToInt32(cateList.Rows[i]["parent_id"].ToString()), cateList.Rows[i]["category_name"].ToString());
            }

            if (string.IsNullOrEmpty(name))
                name += cateList.Rows[i]["category_name"].ToString();

            DataRow dr = dt.NewRow();
            dr["category_id"] = cateList.Rows[i]["category_id"];
            dr["category_name"] = name;
            dr["category_des"] = cateList.Rows[i]["category_des"];

            dt.Rows.Add(dr);
        }

        CategoryList.DataSource = dt;
        CategoryList.DataBind();
    }

    public string GetChildCategory(int category_id, string name)
    {
        string parentName = "";
        DataTable dt = cateBus.GetCategoryByID(category_id);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (Convert.ToInt32(dt.Rows[i]["parent_id"].ToString()) > 0)
                parentName += GetChildCategory(Convert.ToInt32(dt.Rows[i]["parent_id"].ToString()), dt.Rows[i]["category_name"].ToString()) + " > " + name;
            else
                parentName += dt.Rows[i]["category_name"] + " > " + name;
        }
        return parentName;
    }

    protected void delete_Click(object sender, EventArgs e)
    {
        bool check = false;

        foreach (DataListItem item in CategoryList.Items)
        {
            HtmlInputCheckBox ckb = (HtmlInputCheckBox)(item.FindControl("selected"));
            if (ckb.Checked)
            {
                int result = cateBus.DeleteCategory(Convert.ToInt32(ckb.Value));
                if (result == 1)
                    check = true;
                else
                    check = false;
            }
        }

        if (check)
            Response.Redirect("default.aspx?action=categoryList&success=Hoàn tất: Bạn đã xóa Danh mục!");
        else
            Response.Redirect("default.aspx?action=categoryList&warning=Thất bại: Xóa danh mục không thực hiện được!");
    }
}